<?php

/**
 * This is model for map 
 */
class Hoatdong_model extends CI_Model {

    private $_table = "activity";

    function __construct() {
        parent::__construct();
    }

    // Lay danh sach loai trung tam 
    function load_activity()
		{
			$this->db->select('ac.name, ct.name AS ct_name, ac.discription, ct.contact, ac.startday, ac.endday, ac.view, ic.url_image, ac.id_activity, loc.num, loc.street, loc.ward, loc.district, loc.city');
      $this->db->from($this->_table." as ac");
      $this->db->join("center as ct",'ac.id_center = ct.id_center','left');
      $this->db->join('image_center as ic','ac.id_activity = ic.id_activity','left');
      $this->db->join("location as loc","ct.id_location = loc.id_location",'left');
      return $this->db->get();
			//$query = $this->db->get();
			//return $query->result_array();
		}
     function num_rows() {
        $result = $this->db->get($this->_table);
        return $result->num_rows();
    }
    function load_view() {
        $this->db->select("view");
        $query = $this->db->get($this->_table);
        return $query->result_array();
    }

     function add($data)
      {
      $this->db->insert($this->_table,$data);
      }
    function get_by_id($id)
    {
      $this->db->select('ac.name as name_art, ac.discription, ac.startday, '
              . 'ac.endday, ac.view, ac.id_activity, ac.url_image, loc.num, loc.street, loc.ward,'
              . ' loc.district, loc.city, ac.id_center, ct.name, ac.duration_time ');
      $this->db->from($this->_table.' as ac');
      $this->db->join('center as ct','ac.id_center = ct.id_center');
      $this->db->join('image_center as ic','ac.id_activity = ic.id_activity');
      $this->db->join('location as loc','loc.id_location = ct.id_location');
      $this->db->where('ac.id_activity',$id);
      $query = $this->db->get();
      return $query->row_array();
    }
    function get_by_type($id_type)
    {
   
      $this->db->select('ac.name, ct.name AS ct_name, ac.discription, ct.contact, ac.startday, ac.endday, ac.view, ic.url_image, ac.id_activity, loc.num, loc.street, loc.ward, loc.district, loc.city');
      $this->db->from($this->_table." as ac");
      //$this->db->join("article_of_center as aoc", "ar.id_article = aoc.id_article",'left');
      $this->db->join("center as ct", "ct.id_center = ac.id_center",'left');
      //$this->db->join('users as u', 'u.id_user = ar.id_author');
      $this->db->join("image_center as ic", "ac.id_activity = ic.id_activity",'left');
      $this->db->join("location as loc","loc.id_location = ct.id_location","left");
      $this->db->where("ct.id_type_center",$id_type);
        
      $this->db->order_by('ac.startday', 'asc');
        
     // $this->db->limit($limit, $offset);
        
      return  $this->db->get();
    }
    
    // lấy 4 hoạt động mới được cập nhật
    function getNewActivity()
    {
        $this->db->select('ac.name, ac.id_activity, ac.discription, ac.view');
        $this->db->from($this->_table.' as ac');
        $this->db->order_by('startday','DESC');
        $this->db->limit(4);
        $query = $this->db->get();
        if($query->num_rows()){
            return $query->result_array();
        }
        else{
            return NULL;
        }
    }
    // lấy 4 hoạt động được quan tâm nhiều nhất
    function getTopActivity()
    {
        $this->db->select('ac.name, ac.id_activity, ac.discription, ac.view');
        $this->db->from($this->_table.' as ac');
        $this->db->order_by('view','DESC');
        $this->db->limit(4);
        $query = $this->db->get();
        if($query->num_rows()){
            return $query->result_array();
        }
        else{
            return NULL;
        }
    }
    // tăng view
    function increate_view($id,$current_view) {
        $this->db->update($this->_table, array('view'=>$current_view+1), array('id_activity'=>$id));
    }
    
    // Lấy cái hoạt động thuộc 1 trung tâm
    function getActivitiesCenter($id_center){
        $this->db->select('ac.name, ac.id_activity');
        $this->db->from($this->_table.' as ac');
        $this->db->where('id_center', $id_center);
        $this->db->limit(10);
        $query = $this->db->get();
        if($query->num_rows()){
            return $query->result_array();
        }
        else{
            return NULL;
        }
    }
}

?>
